//************creare comentarii************
//--------cssjsXmlChapterComment.js--------
//*****************************************

function commentElementShowMenu(id){
	var paraDiv=document.getElementById(id);

	var para=document.getElementById(id+"p");
	para.className="createNewCommentEditor";
	para.removeAttribute('onclick');

	var addCommentMenuHelder=document.createElement('div');
	addCommentMenuHelder.setAttribute('class','addCommentDiv');
	addCommentMenuHelder.setAttribute('id',id+"addCommentMenuHelder");

	var addCommentDiv=document.createElement('div');
	addCommentDiv.setAttribute('class','addCommentDivElem');
	addCommentDiv.setAttribute('id',id+"addCommentButton");
	addCommentDiv.setAttribute('onclick',"createNewCommentShowEditor('"+id+"');");
	addCommentDiv.appendChild(document.createTextNode("Adauga comentariu!"));

	var addCommentCancelDiv=document.createElement('div');
	addCommentCancelDiv.setAttribute('class','addCommentDivElem');
	addCommentCancelDiv.setAttribute('id',id+"addCommentCancelButton");
	addCommentCancelDiv.setAttribute('onclick',"createNewCommentHideEditor('"+id+"');");
	addCommentCancelDiv.appendChild(document.createTextNode("Cancel"));


	addCommentMenuHelder.appendChild(addCommentDiv);
	addCommentMenuHelder.appendChild(addCommentCancelDiv);

	paraDiv.appendChild(addCommentMenuHelder);
}

function commentElementShowCommentDivs(id){
	var divs=document.getElementById(id);

	if(divs.className == 'commentPartner'){
		divs.className = 'commentPartnerShowComments';
		return true;
	}

	if(divs.className == 'commentPartnerShowComments'){
		divs.className = 'commentPartner';
		return true;
	}
}

function createNewCommentHideEditor(id){
	var div=document.getElementById(id);
	var button=document.getElementById(id+"addCommentMenuHelder");
	var para=document.getElementById(id+"p");

	div.removeChild(button);

	para.removeAttribute("class");
	para.setAttribute('onclick',"commentElementShowMenu('"+id+"');");
}

function createNewCommentShowEditor(id){
	createNewCommentHideEditor(id);

	var hiddenId=document.getElementById('commentElementId');
	hiddenId.value=id;

	DarkenPage();

	//move top
	scroll(0,0);

	var newCommentPanel = document.getElementById('editorElementsComment_panel');
	w = 552;
	xc = Math.round((document.body.clientWidth/2)-(w/2));

	newCommentPanel.style.left = xc + "px";
	newCommentPanel.style.top  = 100 + "px";
	newCommentPanel.style.display = 'block';
}

function createNewCommentOk(chapter){
	var commArr=document.getElementById('createNewCommentTextarea');
	var content=commArr.value;

	var hiddenId=document.getElementById('commentElementId').value;
	var commentId='cmt'+generateRandomNumber();

	var today=new Date();
	var date=today.getDate()+" "+(today.getMonth()+1)+" "+today.getFullYear()+", "+today.getHours()+":"+today.getMinutes();

	if(content !== ''){
		//verifica daca sunt comentarii pt elem dorit
		if(document.getElementById(hiddenId+'commentHelder')){
			//helderul este deja creat
			var helder=document.getElementById(hiddenId+'commentDivs');

			//creare el nou
			var commentDiv=document.createElement('div');
			commentDiv.setAttribute('id', commentId);
			commentDiv.setAttribute('class', 'commentBody');

			//----------------------
			var author=document.getElementById('curentChapterCurrentUser').value;

			//creare elem pt dom
			var authorSpan=document.createElement('span');
			authorSpan.setAttribute('id', commentId+'Author');
			authorSpan.appendChild(document.createTextNode(author));

			var dateSpan=document.createElement('span');
			dateSpan.setAttribute('id', commentId+'Date');
			dateSpan.appendChild(document.createTextNode(date));

			var contentSpan=document.createElement('span');
			contentSpan.setAttribute('id', commentId+'Content');
			contentSpan.appendChild(document.createTextNode(content));

			var editorMenuDiv=document.createElement('div');
			editorMenuDiv.setAttribute('id', commentId+'editorMenu');
			var editSpan=document.createElement('span');
			editSpan.setAttribute('class', 'editCommentMenu');
			editSpan.setAttribute('onclick', "editOwnComment('"+commentId+"');");
			editSpan.appendChild(document.createTextNode('Edit'));
			var deleteSpan=document.createElement('span');
			deleteSpan.setAttribute('class', 'editCommentMenu');
			deleteSpan.setAttribute('onclick', "deleteOwnComment('"+commentId+"');");
			deleteSpan.appendChild(document.createTextNode('Delete'));
			editorMenuDiv.appendChild(editSpan);
			editorMenuDiv.appendChild(deleteSpan);

			//adugare in dom
			commentDiv.appendChild(authorSpan);
			commentDiv.appendChild(document.createTextNode(' , '));
			commentDiv.appendChild(dateSpan);
			commentDiv.appendChild(document.createElement('br'));
			commentDiv.appendChild(contentSpan);
			commentDiv.appendChild(document.createElement('br'));
			commentDiv.appendChild(editorMenuDiv);
			//--------

			//adaugare in dom
			helder.appendChild(commentDiv);
			commArr.value='';

			//adaugare in xmlChapterComments
			friendlyComments.createNewComment(commentId, author, hiddenId, date, content);

			//verificare saveCahnages toolbar
			checkForSaveChangesToolbar();
		}else{
			//creare helder
			var helder=document.createElement('div');
			helder.setAttribute('id', hiddenId+'commentHelder');
			helder.setAttribute('class', 'commentHelder');

			var span=document.createElement('span');
			span.setAttribute('class', 'commentAnnouncement');
			span.setAttribute('onclick', "commentElementShowCommentDivs('"+hiddenId+"commentDivs');");
			span.appendChild(document.createTextNode('++Comentarii'));

			helder.appendChild(span);
			helder.appendChild(document.createElement('br'));

			var commentDivs=document.createElement('div');
			commentDivs.setAttribute('class', 'commentPartner');
			commentDivs.setAttribute('id', hiddenId+'commentDivs');

			var header=document.createElement('div');
			header.setAttribute('class', 'commentHeader');
			header.setAttribute('align', 'left');
			var txt=document.createElement('b');
			txt.appendChild(document.createTextNode('Comentarii...'));
			header.appendChild(txt);
			commentDivs.appendChild(header);

			//creare el nou
			var commentDiv=document.createElement('div');
			commentDiv.setAttribute('id', commentId);
			commentDiv.setAttribute('class', 'commentBody');

			//----------------------
			var author=document.getElementById('curentChapterCurrentUser').value;

			//creare elem pt dom
			var authorSpan=document.createElement('span');
			authorSpan.setAttribute('id', commentId+'Author');
			authorSpan.appendChild(document.createTextNode(author));

			var dateSpan=document.createElement('span');
			dateSpan.setAttribute('id', commentId+'Date');
			dateSpan.appendChild(document.createTextNode(date));

			var contentSpan=document.createElement('span');
			contentSpan.setAttribute('id', commentId+'Content');
			contentSpan.appendChild(document.createTextNode(content));

			var editorMenuDiv=document.createElement('div');
			editorMenuDiv.setAttribute('id', commentId+'editorMenu');
			var editSpan=document.createElement('span');
			editSpan.setAttribute('class', 'editCommentMenu');
			editSpan.setAttribute('onclick', "editOwnComment('"+commentId+"');");
			editSpan.appendChild(document.createTextNode('Edit'));
			var deleteSpan=document.createElement('span');
			deleteSpan.setAttribute('class', 'editCommentMenu');
			deleteSpan.setAttribute('onclick', "deleteOwnComment('"+commentId+"');");
			deleteSpan.appendChild(document.createTextNode('Delete'));
			editorMenuDiv.appendChild(editSpan);
			editorMenuDiv.appendChild(deleteSpan);

			//adugare in dom
			commentDiv.appendChild(authorSpan);
			commentDiv.appendChild(document.createTextNode(' , '));
			commentDiv.appendChild(dateSpan);
			commentDiv.appendChild(document.createElement('br'));
			commentDiv.appendChild(contentSpan);
			commentDiv.appendChild(document.createElement('br'));
			commentDiv.appendChild(editorMenuDiv);
			//--------
			
			commentDivs.appendChild(commentDiv);

			helder.appendChild(commentDivs);

			//adaugare helder in dom
			var editor=document.getElementById('helder');
			editor.insertBefore(helder, document.getElementById(hiddenId).nextSibling);

			//initializare panou comentariu nou
			commArr.value='';

			//adaugare in xmlChapterComments
			friendlyComments.createNewComment(commentId, author, hiddenId, date, content);

			//verificare saveCahnages toolbar
			checkForSaveChangesToolbar();
		}
	}

	var newCommPanel =document.getElementById('editorElementsComment_panel');
	newCommPanel.style.display = 'none';
	LightenPage();
}

function createNewCommentCancel(){
	var txtArrea=document.getElementById('createNewCommentTextarea');
	txtArrea.value='';

	var newParaPanel =document.getElementById('editorElementsComment_panel');
	newParaPanel.style.display = 'none';

	LightenPage();
}

function cleanHelder(){
	var helder=document.getElementById('helder');
	while(helder.hasChildNodes()){
		helder.removeChild(helder.firstChild);
	}
}

function editOwnComment(id){
	var commentDiv=document.getElementById(id);

	var content=document.getElementById(id+'Content');
	var contentTxtArr=document.createElement('textarea');
	contentTxtArr.setAttribute('id', id+'contentTxtArr');
	contentTxtArr.setAttribute('class', 'editOwnComment');
	contentTxtArr.value=content.innerHTML;
	var contentHidden=document.createElement('input');
	contentHidden.setAttribute('type', 'hidden');
	contentHidden.setAttribute('id', id+'hiddenOldValue');
	contentHidden.setAttribute('value', content.innerHTML);


	var oldMenu=document.getElementById(id+'editorMenu');
	var newMenu=document.createElement('div');
	newMenu.setAttribute('id', id+'editorMenu');
	var okSpan=document.createElement('span');
	okSpan.setAttribute('class', 'editCommentMenu');
	okSpan.setAttribute('onclick', "editOwnCommentOk('"+id+"');");
	okSpan.appendChild(document.createTextNode('OK'));
	var cancelSpan=document.createElement('span');
	cancelSpan.setAttribute('class', 'editCommentMenu');
	cancelSpan.setAttribute('onclick', "editOwnCommentCancel('"+id+"');");
	cancelSpan.appendChild(document.createTextNode('Cancel'));
	newMenu.appendChild(okSpan);
	newMenu.appendChild(cancelSpan);

	commentDiv.replaceChild(contentTxtArr, content);
	commentDiv.appendChild(contentHidden);
	commentDiv.replaceChild(newMenu, oldMenu);
}

function editOwnCommentOk(id){
	var commentDiv=document.getElementById(id);
	var elemId=commentDiv.parentNode.id.substr(0, commentDiv.parentNode.id.indexOf("commentDivs"));

	var contentTxtArr=document.getElementById(id+'contentTxtArr');

	var author=document.getElementById(id+'Author').innerHTML;
	var today=new Date();
	var date=today.getDate()+" "+(today.getMonth()+1)+" "+today.getFullYear()+", "+today.getHours()+":"+today.getMinutes();

	//creare elem pt dom
	var authorSpan=document.createElement('span');
	authorSpan.setAttribute('id', id+'Author');
	authorSpan.appendChild(document.createTextNode(author));

	var dateSpan=document.createElement('span');
	dateSpan.setAttribute('id', id+'Date');
	dateSpan.appendChild(document.createTextNode(date));

	var contentSpan=document.createElement('span');
	contentSpan.setAttribute('id', id+'Content');
	contentSpan.appendChild(document.createTextNode(contentTxtArr.value));

	var editorMenuDiv=document.createElement('div');
	editorMenuDiv.setAttribute('id', id+'editorMenu');
	var editSpan=document.createElement('span');
	editSpan.setAttribute('class', 'editCommentMenu');
	editSpan.setAttribute('onclick', "editOwnComment('"+id+"');");
	editSpan.appendChild(document.createTextNode('Edit'));
	var deleteSpan=document.createElement('span');
	deleteSpan.setAttribute('class', 'editCommentMenu');
	deleteSpan.setAttribute('onclick', "deleteOwnComment('"+id+"');");
	deleteSpan.appendChild(document.createTextNode('Delete'));
	editorMenuDiv.appendChild(editSpan);
	editorMenuDiv.appendChild(deleteSpan);

	//eliminare elem vechi
	while(commentDiv.hasChildNodes()){
		commentDiv.removeChild(commentDiv.firstChild);
	}

	//adugare in dom
	commentDiv.appendChild(authorSpan);
	commentDiv.appendChild(document.createTextNode(' , '));
	commentDiv.appendChild(dateSpan);
	commentDiv.appendChild(document.createElement('br'));
	commentDiv.appendChild(contentSpan);
	commentDiv.appendChild(document.createElement('br'));
	commentDiv.appendChild(editorMenuDiv);

	//adaugare in xmlChapterComments
	var newId='cmt'+generateRandomNumber();
	friendlyComments.updateComment(newId, author, elemId, date, contentSpan.innerHTML);

	//verificare saveCahnages toolbar
	checkForSaveChangesToolbar();
}

function editOwnCommentCancel(id){
	var commentDiv=document.getElementById(id);

	var contentTxtArr=document.getElementById(id+'contentTxtArr');
	var oldMenu=document.getElementById(id+'editorMenu');

	var hidenVal=document.getElementById(id+'hiddenOldValue').value;
	var contentSpan=document.createElement('span');
	contentSpan.setAttribute('id', id+'Content');
	contentSpan.appendChild(document.createTextNode(hidenVal));

	var editorMenuDiv=document.createElement('div');
	editorMenuDiv.setAttribute('id', id+'editorMenu');
	var editSpan=document.createElement('span');
	editSpan.setAttribute('class', 'editCommentMenu');
	editSpan.setAttribute('onclick', "editOwnComment('"+id+"');");
	editSpan.appendChild(document.createTextNode('Edit'));
	var deleteSpan=document.createElement('span');
	deleteSpan.setAttribute('class', 'editCommentMenu');
	deleteSpan.setAttribute('onclick', "deleteOwnComment('"+id+"');");
	deleteSpan.appendChild(document.createTextNode('Delete'));
	editorMenuDiv.appendChild(editSpan);
	editorMenuDiv.appendChild(deleteSpan);

	commentDiv.replaceChild(contentSpan, contentTxtArr);
	commentDiv.replaceChild(editorMenuDiv, oldMenu);
}

function deleteOwnComment(id){
	alert("delete"+id);
}

function checkForSaveChangesToolbar(){
	var toolbar=document.getElementById('commentEditorSaveChanges');
	if(toolbar.className == 'editCommentSaveChangesHidden'){
		toolbar.className = 'editCommentSaveChanges';
	}
}


//-----------screen freeze--------------
function editCommentsSaveFinalChanges(chapterId){
	if(friendlyComments.newElemQueue.length !== 0 || friendlyComments.changesQueue.length !== 0){
		DarkenPage();
		showSavingChapterChangesPanel(chapterId);
		prog();
	}else{
		alert("No changes made!");
	}
}

function showSavingChapterChangesPanel(chapterId)
{
	var newsletter_panel = document.getElementById('screenFreeze_panel');
	w = 300;
	h = 160;

	// xc = Math.round((document.body.clientWidth/2)-(w/2));
	// yc = Math.round((document.body.clientHeight/4)-(h/2));
	//xc=Math.round(500-(w/2));
	//	yc=Math.round(250-(h/2));
	xc = Math.round((document.body.clientWidth/2)-(w/2));
	yc = 250;

	newsletter_panel.style.left = xc + "px";
	newsletter_panel.style.top  = yc + "px";
	newsletter_panel.style.display = 'block';
	sendChangeToServer(chapterId);
}

function saveChapterChangesEnd(chapterId)
{
	var newsletter_panel = document.getElementById('screenFreeze_panel');
	newsletter_panel.style.display = 'none';
	LightenPage();
	//console.dir(editorDocument);
	try{
		window.location=document.URL;
	}catch(e){
		alert(e.toString());
	}
}

function DarkenPage()
{
	var page_screen = document.getElementById('screenFreeze_screen');
	page_screen.style.height = document.body.parentNode.scrollHeight + 'px';
	page_screen.style.display = 'block';
}

function LightenPage()
{
	var page_screen = document.getElementById('screenFreeze_screen');
	page_screen.style.display = 'none';
}

//-------progress bar------------
var progressBarInit=0;
var progressBarRatio=0;
function prog()
{
	document.getElementById("progressBar1").innerHTML="";
	document.getElementById("progressBar2").style.width=0;
	progressBarRatio=parseInt((100/(friendlyComments.newElemQueue.length+friendlyComments.changesQueue.length+1))*3);
	progBar();
}

function progBar()
{
	if (progressBarInit <= 300)
	{
		if (progressBarInit>10){
			document.getElementById("progressBar1").innerHTML=parseInt(progressBarInit/3)+"%";
		}
		document.getElementById("progressBar2").style.width=progressBarInit+"px";
		progressBarInit+=progressBarRatio;
	}
}